package cn.juque.jdfsystem.disruptor.event;

import cn.juque.common.base.DisruptorBody;
import cn.juque.common.disruptor.IBusinessEventService;
import cn.juque.jdfsystem.disruptor.bo.FileAddBodyBO;
import cn.juque.jdfsystem.disruptor.bo.FileChangeBodyBO;
import cn.juque.jdfsystem.disruptor.bo.SendMsgBodyBO;
import cn.juque.jdfsystem.handler.HttpMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>RegisterChangeSendMsgDisruptorEventServiceImpl</li>
 * </ul>
 * @date 2023-01-11 17:24:39
 **/
@Slf4j
@Service("sendMsgDisruptorEventService")
public class SendMsgDisruptorEventServiceImpl implements IBusinessEventService {

    public static final String BEAN = "sendMsgDisruptorEventService";

    @Resource
    private HttpMessageHandler httpMessageHandler;

    /**
     * 具体消费者的实现
     *
     * @param disruptorBody 消息体
     */
    @Override
    public void invoke(DisruptorBody disruptorBody) {
        if (disruptorBody instanceof SendMsgBodyBO) {
            SendMsgBodyBO<?> sendMsgBodyBO = (SendMsgBodyBO<?>) disruptorBody;
            switch (sendMsgBodyBO.getMessageTypeEnum()) {
                case REGISTER_CHANGE:
                    this.httpMessageHandler.registerChange();
                    break;
                case CONFIG_CHANGE:
                    this.httpMessageHandler.configChange();
                    break;
                case FILE_ADD:
                    if(sendMsgBodyBO.getPayload() instanceof FileAddBodyBO) {
                        FileAddBodyBO fileAddBodyBO = (FileAddBodyBO) sendMsgBodyBO.getPayload();
                        this.httpMessageHandler.addFile(fileAddBodyBO);
                    }
                    break;
                case FILE_CHANGE:
                    if (sendMsgBodyBO.getPayload() instanceof FileChangeBodyBO) {
                        FileChangeBodyBO fileChangeBodyBO = (FileChangeBodyBO) sendMsgBodyBO.getPayload();
                        this.httpMessageHandler.fileFlagChange(fileChangeBodyBO);
                    }
                    break;
                case FILE_DEL:
                    if(sendMsgBodyBO.getPayload() instanceof List) {
                        List<?> list = (List<?>) sendMsgBodyBO.getPayload();
                        List<String> idList = list.stream().map(Object::toString).collect(Collectors.toList());
                        this.httpMessageHandler.deleteFile(idList);
                    }
                    break;
                case PLATFORM_CHANGE:
                    this.httpMessageHandler.platformChange();
                    break;
                default:
                    break;
            }
        }
    }
}
